#ifndef ESPNOWSEND_H
#define ESPNOWSEND_H
#include "./Config.h"



// 创建结构体实例
DataInfo myData;

// 发送回调函数（可选）
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  if (status == ESP_NOW_SEND_SUCCESS) {

  } else {
    Serial.println("send Fail");
  }
}

//发送数据
void EspNowSend(const uint8_t *peer_addr, const uint8_t *data, size_t len) {
  // 发送数据
  esp_err_t result = esp_now_send(peer_addr, data, len);
  if (result == ESP_OK) {
    //Serial.println("Sent with success");
  } else {
    Serial.println("Error sending the data");
  }
}


//初始化发送
void InitEspNowSend() {


  // 注册发送回调函数（可选）
  esp_now_register_send_cb(OnDataSent);


  esp_now_peer_info_t peerInfo;
  memset(&peerInfo, 0, sizeof(peerInfo));
  memcpy(&peerInfo.peer_addr, broadcastAddress, 6);
  peerInfo.channel = 0;  // 可以根据需要设置通道
  peerInfo.encrypt = false;

  //添加对等设备
  if (esp_now_add_peer(&peerInfo) != ESP_OK) {
    Serial.println("Failed to add peer");
    return;
  }
}

//循环发送函数
void LoopEspNowSend() {

  //转发
  for (int i = 0; i < receivedDataListSize; i++) {
    if (receivedDataList[i].status == 2) {
      //转发已消费的则转发数据，实现桥接
      EspNowSend(broadcastAddress, (uint8_t *)&receivedDataList[i].datainfo, sizeof(receivedDataList[i].datainfo));
      receivedDataList[i].status = 3;  //已转发
    }
  }
}



#endif